/**
 * Copyright (c) 2014 http://www.jieqianhua.com
 */
package com.hllxd.hism.star.basic.service.config.mysql;


import com.hllxd.hism.star.basic.service.config.mysql.datasource.DruidDataSourceConfiguration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@EnableConfigurationProperties(MybatisProperties.class)
@EnableTransactionManagement
@AutoConfigureAfter({DruidDataSourceConfiguration.class, MybatisAutoConfiguration.class})
@MapperScan(
    basePackages = "com.bj58.zp.cdata.star.basic.service.dao.mapper.mis",
//    sqlSessionFactoryRef = "dbAuthSqlSessionFactoryBean",
    sqlSessionTemplateRef = "misSqlSessionTemplate"
)
public class MybatisConfigurationDbMis {

    private static final Logger LOGGER = LoggerFactory.getLogger(MybatisConfigurationDbMis.class);

    @Autowired
    private MybatisProperties properties;

    @Autowired
    private ResourceLoader resourceLoader = new DefaultResourceLoader();

    @Resource(name = "misDataSource")
    private DataSource misDataSource;

    @Bean(name = "misSqlSessionFactoryBean")
    public SqlSessionFactory misSqlSessionFactoryBean() throws Exception {
        return MybatisConfiguration.getSqlSessionFactory(resourceLoader, properties, misDataSource);
    }


    @Bean(name = "misSqlSessionTemplate")
    public SqlSessionTemplate misSqlSessionTemplate(
        @Qualifier(value = "misSqlSessionFactoryBean") SqlSessionFactory misSqlSessionFactoryBean) {
        return new SqlSessionTemplate(misSqlSessionFactoryBean);
    }
}

